/*
 * @Date: 2023-03-04 19:40:00
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-04 20:15:20
 * @FilePath: \A19\backend\src\api\SourceManage\ApiGetPathFiles.ts
 */
import { ApiCall } from "tsrpc";
import { ReqGetPathFiles, ResGetPathFiles } from "../../shared/protocols/SourceManage/PtlGetPathFiles";
import { Global } from '../../Global/index';
import { folders } from '../../shared/types/Folder';
import { findFolder } from '../../utils/FindCurrentFolder';

export default async function (call: ApiCall<ReqGetPathFiles, ResGetPathFiles>) {
    // TODO
    const course = call.req.course
    const root = await Global.collection('folders').findOne({
        course
    })
    if(!root){
        await Global.collection('folders').insertOne({
            folders:[],
            files:[],
            course,
            name:'root'
        })

        call.succ({
            folders:[],
            files:[]
        })
    }
    else{
        const folder = findFolder(call.req.path,root)
        if(!folder) return call.error('文件夹不存在')
        call.succ({
            folders:folder.folders.map(f => f.name),
            files:folder.files
        })
    }
}